<?php
/**
 * @title 79. 单词搜索 - 递归
 * @author start2004
 */

// ini_set("memory_limit", "128M");

// class Solution {
//
//     function find() {
//
//     }
// }

class Solution {

    /**
     * @param String[][] $board
     * @param String $word
     * @return Boolean
     */
    function exist($board, $word) {
        /**
         * @since 2020-09-14 宽高
         */
        $this->w = count($board[0]);
        $this->h = count($board);

        /**
         * @since 2020-09-14 单词长度
         */
        $this->word = $word;
        $this->len = strlen($word);

        /**
         * @since 2020-09-14 遍历找到起点
         */
        for ($i=0; $i<$this->h; $i++){
            for($j=0; $j<$this->w; $j++){
                /**
                 * @since 2020-09-14 找到起点
                 */
                if($board[$i][$j] == $word[0]){
                    /**
                     * @since 2020-09-14 只有1个长度, 匹配成功
                     */
                    if($this->len == 1){
                        return true;
                    } else {
                        /**
                         * @since 2020-09-14 标记
                         */
                        $board[$i][$j] = null;

                        /**
                         * @since 2020-09-14 尝试递归匹配
                         */
                        if(self::find($board,1, $i, $j) === true){
                            return true;
                            break;
                        } else {}

                        /**
                         * @since 2020-09-14 还原, 下次匹配
                         */
                        $board[$i][$j] = $word[0];
                    }
                } else {}
            }
        }

        /**
         * @return
         */
        return false;
    }

    /**
     * @title 匹配
     * @author start2004
     * @since 2020-09-14 Created
     *
     * @param array $board
     * @param int $wordKey
     * @param int $i
     * @param int $j
     * @return bool
     */
    public function find($board=[], $wordKey=0, $i=0, $j=0){
        /**
         * @since 2020-09-14 4个方向去匹配
         */
        $char = $this->word[$wordKey];

        /**
         * @since 2020-09-14 向上
         */
        if($i>=1 AND $board[$i-1][$j] === $char) {
            if($wordKey == $this->len -1){
                return true;
            } else {
                /**
                 * @since 2020-09-14 调整不使用第三方变量
                 */
                $board[$i-1][$j] = null;
                if(self::find($board,$wordKey+1,$i-1, $j) === true){
                    return true;
                } else {}

                /**
                 * @since 2020-09-14 还原
                 */
                $board[$i-1][$j] = $char;
            }
        } else {}

        /**
         * @since 2020-09-14 向下
         */
        if($i<=$this->h -2 AND $board[$i+1][$j] === $char) {
            if($wordKey == $this->len -1){
                return true;
            } else {
                $board[$i+1][$j] = null;
                if(self::find($board, $wordKey+1,$i+1, $j) === true){
                    return true;
                } else {}
                $board[$i+1][$j] = $char;
            }
        } else {}

        /**
         * @since 2020-09-14 向左
         */
        if($j>=1 AND $board[$i][$j-1] === $char) {
            if($wordKey == $this->len -1){
                return true;
            } else {
                $board[$i][$j-1] = null;
                if(self::find($board,$wordKey+1,$i, $j-1) === true){
                    return true;
                } else {}
                $board[$i][$j-1] = $char;
            }
        } else {}

        /**
         * @since 2020-09-14 向右
         */
        if($j<=$this->w -2 AND $board[$i][$j+1] === $char) {
            if($wordKey == $this->len -1){
                return true;
            } else {
                $board[$i][$j+1] = null;
                if(self::find($board,$wordKey+1, $i, $j+1) === true){
                    return true;
                } else {}
                $board[$i][$j+1] = $char;
            }
        } else {}

        /**
         * @return
         */
        return false;
    }
}


/**
 * @url http://127.0.0.1/leetcode/202009/2020.09.13.php
 */
$datas = [
    [
        [
            ['A','B','C','E'],
            ['S','F','C','S'],
            ['A','D','E','E']
        ],
        'ABCCED',
        // 'ABC',
    ],

    // [
    //     [
    //         ['A','B','C','E'],
    //         ['S','F','C','S'],
    //         ['A','D','E','E']
    //     ],
    //     'SEE',
    // ],
    //
    // [
    //     [
    //         ['A','B','C','E'],
    //         ['S','F','C','S'],
    //         ['A','D','E','E']
    //     ],
    //     'ABCD',
    // ],
];

include_once dirname(__DIR__) . DIRECTORY_SEPARATOR ."xhprof.php";
$xhprof = new Xhprof();
foreach ($datas as $data){
    // var_dump($data);

    $obj = new Solution();
    $result = $obj->exist(...$data);
    // $result = $obj->find(...$data);
    var_dump($result);
    // if(count($result)<=20){
    //     var_dump($result);
    // } else {
    //     var_dump(count($result));
    // }
    echo str_repeat("<br>", 3);
}

// foreach ($datas as $data){
//     $obj = new $data[0][0](...$data[1][0]);
//
//     for ($i=1; $i<count($data[0]); $i++){
//         $result = $obj->$data[0][$i](...$data[1][$i]);
//
//         echo $data[0][$i] ."(\"". $data[1][$i][0] ."\") ";
//             if($result === true){
//                 echo "True";
//             } elseif($result === false) {
//                 echo "False";
//             } elseif($result === null) {
//                 echo "Null";
//             } elseif(is_array($result)) {
//                 var_dump($result);
//             } else {
//                 echo $result;
//             }
//         echo PHP_EOL;
//     }
//
//     echo str_repeat(PHP_EOL, 3);
// }
$xhprof->end();
